import React from "react";
import Relay from "react-relay";
import GetEnterpriseListMutation from "../mutation/GetEnterpriseListMutation";
import AddPublishOrder_Item from "./AddPublishOrder_Item";
import ReactKey from "../../../util/ReactKey";
import Util from "../../../util/Util";
import {message} from 'antd';
import {Button} from 'fly';
import PublishOrderMutation from "../mutation/PublishOrderMutation"
import {hashHistory} from "react-router";
export default class PeopleDetail extends React.Component{
    constructor(props){
        super(props);
        this.state={
            enterpriseList:[],
            addpartBItem:[{}],
        };
        this.orderType_obj={
            "1":"成衣",
            "2":"辅料",
            "3":"面料",
            "4":"印染",
            "5":"纱线",
        };
        this.oid = this.props.location.state.oid;
        this.orderType = this.props.location.state.orderType;
        this.account = this.props.location.state.account;
        this.enquiryId = this.props.location.state.enquiryId;
        this.disable=true;
        this.messageFlag=true;
        this.enterpriseId=sessionStorage.getItem("enterpriseId");
    }

    componentWillMount(){
        let addpartBItem = this.props.location.state.addpartBItem;
        if(addpartBItem&&addpartBItem.length>0){
            this.setState({
                addpartBItem:addpartBItem,
            })
        }else {
            this.enterpriseList();
        }
    }
    enterpriseList(){
        let ordertype_num = this.props.location.state.ordertype_num;
        Relay.Store.commitUpdate(
            new GetEnterpriseListMutation({
                serviceType :ordertype_num,
                excludeEnId :this.enterpriseId,
            }),
            {
                onFailure: () => {
                    // console.log("请求失败-乙方企业")
                },
                onSuccess: (response) => {
                    let errorCode = response.getEnterpriseList.data.errorCode;
                    if(errorCode=="0"){
                        let enterpriseList_arr = response.getEnterpriseList.data.enterpriseList;
                        let enterpriseList = [{text:'请选择供应方',value:'0'}];
                        enterpriseList_arr.map((item,index)=>{
                            let enterpriseList_item = {};
                            enterpriseList_item.text = item.enterpriseName;
                            enterpriseList_item.value = item.enterpriseId;
                            enterpriseList.push(enterpriseList_item);
                        });
                        this.enterpriseList = enterpriseList;
                        this.setState({
                            enterpriseList:enterpriseList
                        });
                    }
                }
            }
        )
    }
    addPartbItem(){
        let addpartBItem = this.state.addpartBItem;
        addpartBItem.push({});
        this.setState({
            addpartBItem:addpartBItem
        },()=>{
            this.state.addpartBItem.map((context,index)=>{
                this.refs['addpublishorder'+index].setState({
                    istexteditshow:false
                });
            });
        });
    }

    itemIsEdit(){
        this.state.addpartBItem.map((context,index)=>{
            this.refs['addpublishorder'+index].setState({
                istexteditshow:false
            });
        });
    }

    itemOverEdit(){
        this.state.addpartBItem.map((context,index)=>{
            this.refs['addpublishorder'+index].setState({
                istexteditshow:true
            });
        });
    }

    delteItem(index){
        let enterpriseList = this.state.addpartBItem;
        enterpriseList.splice(index, 1);
        this.setState({
            addpartBItem:enterpriseList
        });
    }


    addPublishOrder_Item(){
        // console.dir(this.state.enterpriseList);
        // console.dir(this.state.addpartBItem);
        return this.state.addpartBItem.map((context,index)=>{
            // console.dir(context)
            return <AddPublishOrder_Item key={ReactKey.key} enterpriseList = {this.state.enterpriseList}
                                         index={index} totallength={this.state.addpartBItem.length}
                                         context={context}
                                         addPartbItem = {this.addPartbItem.bind(this)}
                                         itemIsEdit = {this.itemIsEdit.bind(this)}
                                         itemOverEdit = {this.itemOverEdit.bind(this)}
                                         delteItem = {this.delteItem.bind(this)}
                                         ref={'addpublishorder'+index}/>
        });
    }

    _PublishOrder(){
        // console.dir(this.state.addpartBItem);
        this.disable = true;
        let partblist = [];
        this.state.addpartBItem.map((context,index)=>{
            if(!this.refs['addpublishorder'+index].state.isedit){
                this.disable = false;
            }
            partblist.push(context);
        });
        if(this.disable){
            Relay.Store.commitUpdate(
                new PublishOrderMutation({
                    orderId:this.oid,
                    partBs:JSON.stringify(partblist),
                    enquiryId: this.enquiryId
                }),
                {
                    onFailure: () => {
                        // console.log("请求失败-点击发布")
                    },
                    onSuccess: (response) => {
                        let errorCode = response.publishOrder.publishOrder.errorCode;
                        let value = response.publishOrder.publishOrder.value;
                        let ordertype_num = this.props.location.state.ordertype_num;
                        if(errorCode=="0"){
                            let orderId = response.publishOrder.publishOrder.order["orderId"];
                            let record = response.publishOrder.publishOrder.order;
                            /*record.oid = record.orderId; */
                            if(ordertype_num == "1"){
                                hashHistory.push({pathname:'/OrderIndex/',state:{defaultActiveKey: '1','record':record}});
                            }if(ordertype_num == "3"){
                                hashHistory.push({pathname:'/OrderIndex/',state:{defaultActiveKey: '1','record':record}});
                            }if(ordertype_num == "5"){
                                hashHistory.push({pathname:'/OrderIndex/',state:{defaultActiveKey: '1','record':record}});
                            }if(ordertype_num == "4"){
                                hashHistory.push({pathname:'/OrderIndex/',state:{defaultActiveKey: '1','record':record}});
                            }if(ordertype_num == "2"){
                                hashHistory.push({pathname:'/OrderIndex/',state:{defaultActiveKey: '1','record':record}});
                            }
                        }else {
                            message.warning(value)
                        }
                    }
                }
            )
        }else{
            // message.info('您现在处于编辑状态');
            let this_ = this;
            Util.messageHint(this_,"info","您现在处于编辑状态");
        }
    }

    render(){
        return (
            <div className="createorder_publish">
                <div className="createorder_publish_box">
                    <div className="order_creatOrder_publicOrder_context_borderdiv">
                        <p className="order_creatOrder_publicOrder_context_borderp">订单编号：{this.oid}</p>
                        <p className="order_creatOrder_publicOrder_context_borderp">采购商品：{this.orderType_obj[this.orderType]}</p>
                        <p className="order_creatOrder_publicOrder_context_borderp">订单金额：￥{this.account/100}元</p>
                    </div>

                    {
                        this.addPublishOrder_Item()
                    }
                    <div style={{textAlign:'center',clear:'both',marginTop:'20px',marginBottom: '20px'}}>
                        <Button className=""
                                onClick={this._PublishOrder.bind(this)}>
                            发布</Button>
                    </div>
                </div>
            </div>
        )
    }
}
